package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.mapper.UserMapper;
import com.hqyj.mapper.UserPowerMapper;
import com.hqyj.mapper.UserPowerMapper;
import com.hqyj.pojo.UserPower;
import com.hqyj.service.UserPowerService;
import com.hqyj.utils.RedisUtil;
import com.hqyj.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with Intellij IDEA.
 * User:LiTao
 * Date:2022/8/30
 * Time:14:14
 * Description:
 */
@Service
public class UserPowerServiceImpl implements UserPowerService {

    private static final int CACHE_LIFE_TIME = 60*10;


    @Autowired
    private UserPowerMapper userPowerMapper;
    @Autowired
    private RedisUtil redisUtil;
    //全查，分页
    @Override
    public Map<String, Object> selectAll(int pageNum, int pageSize,String remark) {

        String key = "userPower_page" + pageNum;
        Map<String, Object> map = new HashMap<>();

        if (!"".equals(remark)){
            PageHelper.startPage(pageNum,pageSize);
            // 调用userPowerMapper
            List<UserPower> userList = userPowerMapper.selectAll(remark);
            PageInfo<UserPower> userPageInfo = new PageInfo<>(userList);
            //要返回一个map，所以创建一个map
            map.put("code",200);
            map.put("paging",userPageInfo);
        }

        else if (!redisUtil.hasKey(key)){
            //开启分页
            PageHelper.startPage(pageNum,pageSize);
            // 调用userPowerMapper
            List<UserPower> userList = userPowerMapper.selectAll(remark);
            PageInfo<UserPower> userPageInfo = new PageInfo<>(userList);
            //要返回一个map，所以创建一个map
            map.put("code",200);
            map.put("paging",userPageInfo);
            redisUtil.hmset(key,map,CACHE_LIFE_TIME);
        }else {
            map = redisUtil.hmget(key);
        }

        return map;
    }

    //添加
    @Override
    public Map<String, Object> add(String power, String remark) {
        int num = userPowerMapper.add(power, remark);
        Map<String, Object> map = new HashMap<>();
        if (num>0){
            deleteCache();
            map.put("code",200);
            return map;
        }else {
            map.put("code",500);
            return map;
        }
    }

    //回显
    @Override
    public Map<String, Object> update(int id) {
        UserPower user = userPowerMapper.update(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("userInfo",user);
        return map;
    }

    // 修改
    @Override
    public Map<String, Object> updateUser(UserPower userPower) {
        int num = userPowerMapper.updateUser(userPower);
        Map<String, Object> map = new HashMap<>();
        if (num>0){
            deleteCache();
            map.put("code",200);
            return map;
        }else {
            map.put("code",500);
            return map;
        }
    }

    //删除
    @Override
    public Map<String, Object> deleteUser(int id) {
        int num = userPowerMapper.deleteUser(id);
        Map<String, Object> map = new HashMap<>();
        if (num>0){
            deleteCache();
            map.put("code",200);
            return map;
        }else {
            map.put("code",500);
            return map;
        }
    }

    //获取所有权限
    @Override
    public Map<String, Object> getAllPower() {
        List<UserPower> userPowerList = userPowerMapper.getAll();
        if (userPowerList != null){
            return ResultUtil.success("查询成功",userPowerList);
        }
        return ResultUtil.error("查询失败");
    }

    public void deleteCache(){
        redisUtil.del(redisUtil.keysLike("userPower_page"));
    }
}
